﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.Transaction
{
	public partial class NeedChargedForSaleForm : BaseForm
	{
		private List<MainBill> mainBillList;
		private List<RecipientsBill> recipientBillList;
		private List<SalesBill> salesBillList;

		public NeedChargedForSaleForm()
		{
			InitializeComponent();

			ucMainBill.InitializeColumnHeader(typeof(Models.MainBill));
			ucDetailBill.InitializeColumnHeader(typeof(Models.RecipientsBill));
			ucSalesBill.InitializeColumnHeader(typeof(Models.SalesBill));

			ucMainBill.dgvData.SelectionChanged += new EventHandler(dgvData_SelectionChanged);
			ucMainBill.dgvData.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dgvData_DataBindingComplete);
			ucMainBill.dgvData.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvData_CellFormatting);
			ucDetailBill.dgvData.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvData2_CellFormatting);
			ucSalesBill.dgvData.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvData3_CellFormatting);
			ucSalesBill.dgvData.DataBindingComplete +=new DataGridViewBindingCompleteEventHandler(dgvData2_DataBindingComplete);

			DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
			col.HeaderText = "车主";
			col.ReadOnly = true;
			col.Name = "CustomerName";
			col.DataPropertyName = "CustomerName";
			DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
			col2.HeaderText = "合计";
			col2.ReadOnly = true;
			col2.DataPropertyName = "Total";
			col2.Name = "Total";
			ucMainBill.dgvData.Columns.Add(col);
			ucMainBill.dgvData.Columns.Add(col2);
		}

		void dgvData_SelectionChanged(object sender, EventArgs e)
		{
			try
			{
				if (ucMainBill.dgvData.SelectedRows.Count > 0)
				{
					ucDetailBill.DataSource = null;
					CarDBContext db = new CarDBContext();
					string mainBillCode = ucMainBill.dgvData.SelectedRows[0].Cells["MainBillCode"].Value.ToString();
					recipientBillList = db.RecipientsBill.Include("Good").Include("CarType").Include("Staff").Where(p => p.MainBillCode == mainBillCode).OrderBy(p => p.ID).ToList();
					ucDetailBill.DataSource = recipientBillList;

					decimal total = 0;
					foreach (var item in recipientBillList)
					{
						total += item.SalesAmount;
					}
					lbPartTotal.Text = total.ToString("f1") + "元";
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		void dgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			string str = ucMainBill.dgvData.Columns[e.ColumnIndex].Name;
			switch (str)
			{
				case "CarID":
					{
						e.FormattingApplied = true;
						e.Value = mainBillList[e.RowIndex].Car.CarName; break;
					}
				case "CustomerName":
					{
						e.FormattingApplied = true;
						CarDBContext db = new CarDBContext();
						string carNO = mainBillList[e.RowIndex].CarNO;
						CarInfo car = db.CarInfo.Include("Customer").First(p => p.NO == carNO);
						e.Value = car.Customer.Name; break;
					}
			}
		}
		void dgvData2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			string str = ucDetailBill.dgvData.Columns[e.ColumnIndex].Name;
			switch (str)
			{
				case "GoodID":
					{
						e.FormattingApplied = true;

						e.Value = recipientBillList[e.RowIndex].Good.Code; break;
					}
				case "StaffID":
					{
						e.FormattingApplied = true;

						e.Value = recipientBillList[e.RowIndex].Staff.Name; break;
					}
				case "CarTypeID":
					{
						e.FormattingApplied = true;

						e.Value = recipientBillList[e.RowIndex].CarType.Name; break;
					}
			}
		}
		void dgvData3_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			string str = ucSalesBill.dgvData.Columns[e.ColumnIndex].Name;
			switch (str)
			{
				case "CustomerID":
					{
						e.FormattingApplied = true;

						e.Value = salesBillList[e.RowIndex].Customer.Name; break;
					}
				case "GoodID":
					{
						e.FormattingApplied = true;

						e.Value = salesBillList[e.RowIndex].Good.Code; break;
					}
				case "StaffID":
					{
						e.FormattingApplied = true;

						e.Value = salesBillList[e.RowIndex].Staff.Name; break;
					}
				case "CarTypeID":
					{
						e.FormattingApplied = true;

						e.Value = salesBillList[e.RowIndex].CarType.Name; break;
					}
			}
		}
		void dgvData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
		{
			CarDBContext db = new CarDBContext();
			decimal total = 0;
			foreach (DataGridViewRow item in ucMainBill.dgvData.Rows)
			{
				string mainBillCode = ucMainBill.dgvData.Rows[item.Index].Cells["MainBillCode"].Value.ToString();

				recipientBillList = db.RecipientsBill.Include("Good").Include("CarType").Include("Staff").Where(p => p.MainBillCode == mainBillCode).OrderBy(p => p.ID).ToList();

				decimal dec = 0;
				foreach (var bill in recipientBillList)
				{
					dec += bill.SalesAmount;
				}
				item.Cells["Total"].Value = dec.ToString("f1");

				total += dec;
			}

			lbTotal.Text = total.ToString("f1") + "元";
		}
		void dgvData2_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
		{
			decimal total = 0;
			foreach (DataGridViewRow item in ucSalesBill.dgvData.Rows)
			{
				total += Convert.ToDecimal(item.Cells["SalesAmount"].Value);
			}

			lbSalesTotal.Text = total.ToString("f1") + "元";
		}


		private void NeedChargedForm_Load(object sender, EventArgs e)
		{
			try
			{
				DataBindForMainBill();
				DataBindForSalesBill();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void DataBindForMainBill()
		{
			try
			{
				CarDBContext db = new CarDBContext();
				mainBillList = db.MainBill.Include("Car").Where(p => p.IsSettle == 0 && p.State == "已完成").OrderBy(p => p.ID).ToList();
				ucMainBill.DataSource = mainBillList;

			}
			catch (Exception ex)
			{

				throw ex;
			}
		}

		private void btnSettle_Click(object sender, EventArgs e)
		{
			try
			{
				if (ucMainBill.dgvData.SelectedRows.Count > 0)
				{
					string mainBillCode = ucMainBill.dgvData.SelectedRows[0].Cells["MainBillCode"].Value.ToString();
					SettleForm form = new SettleForm(mainBillCode);
					if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
					{
						DataBindForMainBill();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		private void DataBindForSalesBill()
		{
			try
			{
				CarDBContext db = new CarDBContext();
				salesBillList = db.SalesBill.Include("Customer").Include("Good").Include("CarType").Include("Staff").Where(p => p.IsSettle == "否").OrderBy(p => p.ID).ToList();
				ucSalesBill.DataSource = salesBillList;
			}
			catch (Exception ex)
			{

				throw ex;
			}
		}

		private void btnSettleSales_Click(object sender, EventArgs e)
		{
			try
			{
				if (ucSalesBill.dgvData.SelectedRows.Count > 0)
				{
					if (MessageBox.Show(this, "确定要结算吗？", "确认", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
					{
						CarDBContext db = new CarDBContext();
						string code = ucSalesBill.dgvData.SelectedRows[0].Cells["Code"].Value.ToString();
						db.SalesBill.First(p => p.Code == code).IsSettle = "是";
						db.SaveChanges();

						DataBindForSalesBill();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
	}
}
